/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddMovieIdDialog.java
 *
 * Created on Aug 21, 2010, 12:23:57 PM
 */

package yayman;

import java.util.HashMap;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import java.util.logging.*;
import org.jdesktop.application.Action;

import static yayman.MovieXmlTools.*;

public class AddMovieIdDialog extends javax.swing.JDialog {

    private YAYManView main;
    private static Logger logger = Logger.getLogger("yayman");

    /** Creates new form AddMovieIdDialog */
    public AddMovieIdDialog(YAYManView parent) {
        super(parent.getFrame(), true);
        initComponents();
        main = parent;

        ArrayList<MovieSearchPluginItem> allPlugins = JukeboxInterface.getSearchPluginsArray();

        HashMap<String,MovieSearchPluginItem> ids = main.getMovieIds();

        DefaultComboBoxModel model = new DefaultComboBoxModel();

        for (int i=0; i < allPlugins.size(); i++) {
            if (!ids.containsKey(allPlugins.get(i).toString())) model.addElement(allPlugins.get(i));
        }

        if (model.getSize() > 0) {
            cmbDb.setModel(model);
        } else {
            logger.warning("All video IDs already specified.");
            closeDialog();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbDb = new javax.swing.JComboBox();
        txtKey = new javax.swing.JTextField();
        addBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getResourceMap(AddMovieIdDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cmbDb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbDb.setName("cmbDb"); // NOI18N

        txtKey.setText(resourceMap.getString("txtKey.text")); // NOI18N
        txtKey.setName("txtKey"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getActionMap(AddMovieIdDialog.class, this);
        addBtn.setAction(actionMap.get("AddMovieId")); // NOI18N
        addBtn.setText(resourceMap.getString("addBtn.text")); // NOI18N
        addBtn.setName("addBtn"); // NOI18N

        cancelBtn.setAction(actionMap.get("closeDialog")); // NOI18N
        cancelBtn.setText(resourceMap.getString("cancelBtn.text")); // NOI18N
        cancelBtn.setName("cancelBtn"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmbDb, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtKey, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(addBtn)
                        .addGap(18, 18, 18)
                        .addComponent(cancelBtn)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmbDb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn)
                    .addComponent(addBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void closeDialog() {
        this.setVisible(false);
        this.dispose();
    }

    @Action
    public void AddMovieId() {
        if (!txtKey.getText().isEmpty()) {
            main.addSelectedMovieId((MovieSearchPluginItem)cmbDb.getSelectedItem(), txtKey.getText());
        }
        closeDialog();
    }

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JComboBox cmbDb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtKey;
    // End of variables declaration//GEN-END:variables

}
